package com.wilmer.paperAnswers.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wilmer.common.utils.DateUtils;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.options.domain.YwOptions;
import com.wilmer.options.service.IYwOptionsService;
import com.wilmer.paperResults.domain.GetInfoBackVO;
import com.wilmer.paperResults.domain.YwPaperResults;
import com.wilmer.paperResults.mapper.YwPaperResultsMapper;
import com.wilmer.papers.domain.YwPapers;
import com.wilmer.papers.service.IYwPapersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wilmer.paperAnswers.mapper.YwPaperStudentAnswersMapper;
import com.wilmer.paperAnswers.domain.YwPaperStudentAnswers;
import com.wilmer.paperAnswers.service.IYwPaperStudentAnswersService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 答题记录Service业务层处理
 *
 * @author zl
 * @date 2024-04-23
 */
@Service
public class YwPaperStudentAnswersServiceImpl implements IYwPaperStudentAnswersService
{
    @Autowired
    private YwPaperStudentAnswersMapper ywPaperStudentAnswersMapper;
    @Autowired
    private IYwPapersService ywPapersService;
    @Autowired
    private IYwOptionsService ywOptionsService;
    @Autowired
    private YwPaperResultsMapper ywPaperResultsMapper;


    @Override
    public YwPaperStudentAnswers selectYwPaperStudentAnswersByAnswerId(Long answerId) {
        return this.ywPaperStudentAnswersMapper.selectYwPaperStudentAnswersByAnswerId(answerId);
    }

    @Override
    public List<YwPaperStudentAnswers> selectYwPaperStudentAnswersList(YwPaperStudentAnswers ywPaperStudentAnswers) {
        return this.ywPaperStudentAnswersMapper.selectYwPaperStudentAnswersList(ywPaperStudentAnswers);
    }

    @Override
    public GetInfoBackVO getInfoBack(Long paperId,Long userId) {
        Map<String, Long> map = new HashMap();
        map.put("paperId", paperId);
        if (userId!=null){
            map.put("userId", userId);
        }else {
            map.put("userId", SecurityUtils.getUserId());
        }
        List<YwPaperStudentAnswers> infoBack = this.ywPaperStudentAnswersMapper.getInfoBack(map);

        for(int i = 0; i < infoBack.size(); ++i) {
            List<YwOptions> ywOptions = this.ywOptionsService.selectYwOptionsListByQuestionId((infoBack.get(i)).getQuestionId());
            (infoBack.get(i)).setOptionsList(ywOptions);
        }

        YwPapers ywPapers = this.ywPapersService.selectYwPapersByPaperId(paperId);
        GetInfoBackVO getInfoBackVO = new GetInfoBackVO();
        getInfoBackVO.setYwPapers(ywPapers);
        getInfoBackVO.setPaperStudentAnswersList(infoBack);
        return getInfoBackVO;
    }

    @Override
    public int insertYwPaperStudentAnswers(YwPaperStudentAnswers ywPaperStudentAnswers) {
        ywPaperStudentAnswers.setCreateTime(DateUtils.getNowDate());
        return this.ywPaperStudentAnswersMapper.insertYwPaperStudentAnswers(ywPaperStudentAnswers);
    }

    @Override
    @Transactional
    public int awardingOfPoints(YwPaperStudentAnswers ywPaperStudentAnswers) {
        try {
            YwPaperStudentAnswers ywPaperStudentAnswers1 = this.ywPaperStudentAnswersMapper.selectYwPaperStudentAnswersByAnswerId(ywPaperStudentAnswers.getAnswerId());
            ywPaperStudentAnswers1.setIsCorrect(ywPaperStudentAnswers.getIsCorrect());
            ywPaperStudentAnswers1.setIncomeScore(ywPaperStudentAnswers.getScore());
            this.ywPaperStudentAnswersMapper.updateYwPaperStudentAnswers(ywPaperStudentAnswers1);
            Map<String,Long> map = new HashMap<>();
            map.put("paperId",ywPaperStudentAnswers1.getPaperId());
            map.put("studentId",ywPaperStudentAnswers1.getStudentId());
            YwPaperResults ywPaperResults = this.ywPaperResultsMapper.selectPaperResultsByPaperIdAndStudentId(map);
            ywPaperResults.setTotalScore(ywPaperResults.getTotalScore().add(ywPaperStudentAnswers.getScore()));
            this.ywPaperResultsMapper.updateYwPaperResults(ywPaperResults);
            return 1;
        } catch (Exception var4) {
            return 0;
        }
    }

    @Override
    public int updateYwPaperStudentAnswers(YwPaperStudentAnswers ywPaperStudentAnswers) {
        ywPaperStudentAnswers.setUpdateTime(DateUtils.getNowDate());
        return this.ywPaperStudentAnswersMapper.updateYwPaperStudentAnswers(ywPaperStudentAnswers);
    }

    @Override
    public int deleteYwPaperStudentAnswersByAnswerIds(Long[] answerIds) {
        return this.ywPaperStudentAnswersMapper.deleteYwPaperStudentAnswersByAnswerIds(answerIds);
    }

    @Override
    public int deleteYwPaperStudentAnswersByAnswerId(Long answerId) {
        return this.ywPaperStudentAnswersMapper.deleteYwPaperStudentAnswersByAnswerId(answerId);
    }
//    @Autowired
//    private YwPaperStudentAnswersMapper ywPaperStudentAnswersMapper;
//
//    /**
//     * 查询答题记录
//     *
//     * @param answerId 答题记录主键
//     * @return 答题记录
//     */
//    @Override
//    public YwPaperStudentAnswers selectYwPaperStudentAnswersByAnswerId(Long answerId)
//    {
//        return ywPaperStudentAnswersMapper.selectYwPaperStudentAnswersByAnswerId(answerId);
//    }
//
//    /**
//     * 查询答题记录列表
//     *
//     * @param ywPaperStudentAnswers 答题记录
//     * @return 答题记录
//     */
//    @Override
//    public List<YwPaperStudentAnswers> selectYwPaperStudentAnswersList(YwPaperStudentAnswers ywPaperStudentAnswers)
//    {
//        return ywPaperStudentAnswersMapper.selectYwPaperStudentAnswersList(ywPaperStudentAnswers);
//    }
//
//    /**
//     * 新增答题记录
//     *
//     * @param ywPaperStudentAnswers 答题记录
//     * @return 结果
//     */
//    @Override
//    public int insertYwPaperStudentAnswers(YwPaperStudentAnswers ywPaperStudentAnswers)
//    {
//        ywPaperStudentAnswers.setCreateTime(DateUtils.getNowDate());
//        return ywPaperStudentAnswersMapper.insertYwPaperStudentAnswers(ywPaperStudentAnswers);
//    }
//
//    /**
//     * 修改答题记录
//     *
//     * @param ywPaperStudentAnswers 答题记录
//     * @return 结果
//     */
//    @Override
//    public int updateYwPaperStudentAnswers(YwPaperStudentAnswers ywPaperStudentAnswers)
//    {
//        ywPaperStudentAnswers.setUpdateTime(DateUtils.getNowDate());
//        return ywPaperStudentAnswersMapper.updateYwPaperStudentAnswers(ywPaperStudentAnswers);
//    }
//
//    /**
//     * 批量删除答题记录
//     *
//     * @param answerIds 需要删除的答题记录主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwPaperStudentAnswersByAnswerIds(Long[] answerIds)
//    {
//        return ywPaperStudentAnswersMapper.deleteYwPaperStudentAnswersByAnswerIds(answerIds);
//    }
//
//    /**
//     * 删除答题记录信息
//     *
//     * @param answerId 答题记录主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwPaperStudentAnswersByAnswerId(Long answerId)
//    {
//        return ywPaperStudentAnswersMapper.deleteYwPaperStudentAnswersByAnswerId(answerId);
//    }
}
